package com.runen.infinitemovement.tool;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.runen.infinitemovement.R;
import com.runen.rxnetlibrary.utils.LogUtil;
import com.runen.rxnetlibrary.utils.ToastUtils;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Create by LiaoDuanHong
 * Create on 2020/10/17 15:09
 */
public class ShareUtils {

    public static ShareUtils getInstance(Context context, String type, String title, String describe, String imageUrl) {
        return new ShareUtils(context, type, title, describe, imageUrl);
    }

    private ShareUtils(Context context, String type, String title, String describe, String imageUrl) {
        switch (type) {
            case "Wechat":
                shaer(context, Wechat.NAME, title, describe, imageUrl);
                break;
            case "WechatMoments":
                shaer(context, WechatMoments.NAME, title, describe, imageUrl);
                break;
            case "QQ":
                shaer(context, QQ.NAME, title, describe, imageUrl);
                break;
            case "QZONE":
                shaer(context, QZone.NAME, title, describe, imageUrl);
                break;
        }
    }

    private void shaer(Context context, String name, String title, String describe, String imageUrl) {
        Platform platform = ShareSDK.getPlatform(name);
        Platform.ShareParams params = new Platform.ShareParams();
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ico_logo);
        params.setShareType(Platform.SHARE_WEBPAGE);
        params.setImageData(bitmap);
        params.setTitle(title);
        params.setText(describe);
        LogUtil.e("QZone", name);
        if ("QZone".equals(name)) {
            params.setTitleUrl(imageUrl);
        } else {
            params.setUrl(imageUrl);
        }
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                Log.e("ShareSDK", "onComplete() ");
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                Log.e("ShareSDK", "onError() ");
                ToastUtils.showShort(throwable.getMessage());
            }

            @Override
            public void onCancel(Platform platform, int i) {
                Log.e("ShareSDK", "onCancel() ");

            }
        });
        platform.share(params);
    }

}
